package cn.dansj.common.utils.transfer;

import java.lang.reflect.Array;
import java.util.*;
import java.util.regex.Pattern;

public abstract class Verification {
    public static boolean checkNull(Object obj) {
        if (obj == null) return true;
        if (obj instanceof Optional<?>) return !((Optional<?>) obj).isPresent();
        if (obj instanceof CharSequence) return ((CharSequence) obj).toString().trim().isEmpty();
        if (obj.getClass().isArray()) return Array.getLength(obj) == 0;
        if (obj instanceof Collection<?>) return ((Collection<?>) obj).isEmpty();
        if (obj instanceof Map<?, ?>) return ((Map<?, ?>) obj).isEmpty();
        return false;
    }

    public static boolean checkNotNull(Object val) {
        return !checkNull(val);
    }

    public static boolean checkAnyNull(Object... val) {
        for (Object v : val) if (checkNull(v)) return true;
        return false;
    }

    public static boolean checkAnyNotNull(Object... val) {
        for (Object v : val) if (checkNotNull(v)) return true;
        return false;
    }

    public static boolean checkAllNull(Object... val) {
        for (Object v : val) if (checkNotNull(v)) return false;
        return true;
    }

    public static boolean checkAllNotNull(Object... val) {
        for (Object v : val) if (checkNull(v)) return false;
        return true;
    }

    // 正则表达式，判断字符串不被单引号或双引号包裹
    private static final Pattern NOT_QUOTED_PATTERN = Pattern.compile("^(?!['\"]).*(?<!['\"])$");

    public static boolean isNotQuoted(String str) {
        return NOT_QUOTED_PATTERN.matcher(str).matches();
    }

    public static boolean isQuoted(String str) {
        return !isNotQuoted(str);
    }
}
